package com.signway.swmediaplayerdemo;

import static com.signway.swmediaplayerdemo.MainActivity.getVideoPath;
import static com.signway.swmediaplayerdemo.MainActivity.hideBottomUIMenu;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.signway.swmediaplayer.SWMediaPlayer;
import com.signway.swmediaplayer.utils.SystemProperties;
import com.signway.swmediaplayerdemo.databinding.ActivitySplitBinding;
import com.signway.swmediaplayerdemo.databinding.ActivitySubtitleBinding;

import java.io.IOException;

public class SplitActivity extends AppCompatActivity {
    private static final String TAG = SplitActivity.class.getName();
    private ActivitySplitBinding binding;
    private SWMediaPlayer mSWMediaPlayer;
    private int MODE = SWMediaPlayer.MEDIA_COMPLETION_PAUSE;

    private SurfaceView mSurfaceView = null;

    private SWMediaPlayer.OnCompletionListener mOnCompletionListener =
            (mp, lastRenderTimestampNs) -> {
        switch (MODE) {
            case SWMediaPlayer.MEDIA_COMPLETION_CONTINUE:
                MainActivity.VideoRes res = getVideoPath();
                try {
                    mSWMediaPlayer.setNextFilePath(res.path);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
        }
        return MODE;
    };

    private SWMediaPlayer.OnPreparedListener mOnPreparedListener =
            mp -> mp.start();

    private SWMediaPlayer.OnInfoListener mOnInfoListener = (mp, what, extra) -> {
        switch (what) {
            case SWMediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
                Log.d(TAG, "MEDIA_INFO_VIDEO_RENDERING_START");
                break;
            case SWMediaPlayer.MEDIA_INFO_VIDEO_RENDERING_END:
                Log.d(TAG, "MEDIA_INFO_VIDEO_RENDERING_END");
                switch (MODE) {
                    case SWMediaPlayer.MEDIA_COMPLETION_STOP:
                        initPlayer();
                        try {
                            mSWMediaPlayer.prepare();
                        } catch (IOException | InterruptedException e) {
                            e.printStackTrace();
                        }
                        break;
                    case SWMediaPlayer.MEDIA_COMPLETION_PAUSE:
                        MainActivity.VideoRes res = getVideoPath();
                        try {
                            mSWMediaPlayer.setNextFilePath(res.path);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        try {
                            mSWMediaPlayer.prepare();
                        } catch (IOException | InterruptedException e) {
                            e.printStackTrace();
                        }
                        break;
                }
                break;
        }
        return true;
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySplitBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        hideBottomUIMenu(this);
        MODE = getIntent().getIntExtra("MODE", MODE);

        mSWMediaPlayer = new SWMediaPlayer();
        mSWMediaPlayer.setOnPreparedListener(mOnPreparedListener);
        mSWMediaPlayer.setOnInfoListener(mOnInfoListener);
        mSWMediaPlayer.setOnCompletionListener(mOnCompletionListener);

        initPlayer();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mSWMediaPlayer.release();
    }

    private void initPlayer() {
        if (mSurfaceView == null) {
            mSurfaceView = new SurfaceView(this);
            mSurfaceView.setLayoutParams(new RelativeLayout.LayoutParams(2160, 1215));
            binding.surfaceLayout.addView(mSurfaceView);
        }

        mSurfaceView.getHolder().setFormat(PixelFormat.TRANSPARENT);
        mSurfaceView.getHolder().addCallback(new SurfaceCallBack(this, mSWMediaPlayer));
        mSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        if (mSWMediaPlayer != null) {
            try {
                mSWMediaPlayer.reset();
                MainActivity.VideoRes res = getVideoPath();
                mSWMediaPlayer.setDataSource(res.path);
                mSWMediaPlayer.setDisplay(mSurfaceView.getHolder());
                Log.d(TAG,  "mPlayer.getDuration() " + mSWMediaPlayer.getDuration());
            } catch (Exception e) {
                e.printStackTrace();
                mSWMediaPlayer.release();
                Toast.makeText(this, "无法播放", Toast.LENGTH_LONG).show();
            }
        }
    }

    private class SurfaceCallBack implements SurfaceHolder.Callback {
        public SurfaceCallBack(Context context, SWMediaPlayer player) {
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            Log.d(TAG, "surfaceCreated " + holder.toString());
            try {
                mSWMediaPlayer.prepare();
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            Log.d(TAG, "surfaceChanged width " + width + ", height" + height);
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            Log.d(TAG, "surfaceDestroyed");
        }
    }
}